package com.engine.thread;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.api.taobao.Itemcatget;
import com.api.taobao.ItemsSearch;
import com.api.taobao.Paginator;
import com.biz.jdbc.CatsDao;
import com.biz.jdbc.CatsDao.Cats;
import com.biz.result.ItemSearchResult;
import com.engine.QueueInstance;
import com.taobao.api.ApiException;
import com.taobao.api.domain.ItemCat;
import com.taobao.api.response.ItemcatsGetResponse;

/**
 * @author liuchao
 * @version $Id: SearchAPIThread.java,v 0.1 2012-9-21 下午9:49:58 liuchao Exp $
 */
public class SearchAPIThread implements Runnable{
    protected Log                       log = LogFactory.getLog(this.getClass());
    private CatsDao catsDao;
    
    private Itemcatget itemCatGet;
    
    
    
    public SearchAPIThread(){
        catsDao = new CatsDao();
        itemCatGet = new Itemcatget();
    }
    
    @Override
    public void run() {
        try {
            while(true){
                //如果队里空执行 search API
                if(QueueInstance.getInstance().getDataQueue().size() > 0){
                    ItemSearchResult itemQueueDate = QueueInstance.getInstance().getDataQueue().take();
                    if( null != itemQueueDate){
                        ItemsSearchCallable searchCallable = new ItemsSearchCallable( itemQueueDate.getCid(),itemQueueDate.getPageNo() );
                        ItemSearchResult result = QueueInstance.getInstance().getCacheExec().submit(searchCallable).get();
                        if( result.getErrorNO() != null ){
                            log.info("----------------error info:"+result.getErrorNO()+"----------------");
                            QueueInstance.getInstance().getDataQueue().offer(result);
                        }
                    }
                } else{
                    List<Cats> cats = catsDao.getCats();
                    for(Cats cat : cats){
                            //调用search API
                            @SuppressWarnings("static-access")
                            ItemcatsGetResponse res = itemCatGet.getItemCat(cat.getCid().toString());
                            for(ItemCat itemcat : res.getItemCats()){
                                System.out.println("cat.is parent:"+itemcat.getIsParent());
                                if( !itemcat.getIsParent()){
                                    //得到类目的总条数
                                    Long totalResult = ItemsSearch.getTotalResult( itemcat.getCid() );
                                    Paginator paginator = new Paginator(200);
                                    paginator.setItems(totalResult.intValue());
                                    int pageSize = paginator.getPages();
                                    if( pageSize > 10240){
                                        pageSize = 10240;
                                    }
                                    System.out.println("pagesize:"+pageSize);
                                    for( int i=1;i<= pageSize; i++){
                                        log.info("----------------类目 "+cat.getCid()+"第"+i+"页----------------");
                                        //统一提交、统一返回结果。
                                        ItemsSearchCallable searchCallable = new ItemsSearchCallable( cat.getCid(),new Long(i));
                                        ItemSearchResult result = QueueInstance.getInstance().getCacheExec().submit(searchCallable).get();
                                        if( result.getErrorNO() != null ){
                                            log.info("----------------error info:"+result.getErrorNO()+"----------------");
                                            QueueInstance.getInstance().getDataQueue().offer(result);
                                        }
                                    }
                                }
                            }
                    }
                }
                Thread.sleep(1000*30);
            }
           
        } catch (SQLException e) {
        }
        catch (ApiException e){
        }
        catch (Exception e){
        }
    }

}
